package com.teacher.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teacher.common.CommonResult;
import com.teacher.common.JwtTokenUtil;
import com.teacher.mapper.StuClassMapper;
import com.teacher.pojo.dto.stuclass.StuClassGetDto;
import com.teacher.pojo.entity.*;
import com.teacher.pojo.vo.stuclass.StuClassGetVo;
import com.teacher.service.IClassTeacherService;
import com.teacher.service.IPerStudentService;
import com.teacher.service.IPerTeacherService;
import com.teacher.service.IStuClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2021-07-17
 */
@Service
public class StuClassServiceImpl extends ServiceImpl<StuClassMapper, StuClass> implements IStuClassService {

    @Value("${role.classMonitor}")
    private String classMonitor;

    @Value("${role.admin}")
    private String admin;

    @Value("${role.student}")
    private String student;

    @Value("${role.officeMonitor}")
    private String officeMonitor;

    @Value("${role.office}")
    private String office;

    @Value("${role.subjectMonitor}")
    private String subjectMonitor;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private IPerStudentService iPerStudentService;

    @Autowired
    private IPerTeacherService iPerTeacherService;

    @Autowired
    private IClassTeacherService iClassTeacherService;

    /**
     * 查询班级
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<IPage<StuClassGetVo>> getPage(StuClassGetDto dto) {
        Page<StuClassGetVo> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        IPage<StuClassGetVo> iPage = baseMapper.getPage(page, dto);
        return CommonResult.success(iPage);
    }

    /**
     * 远程搜索班级-带权限
     *
     * @param name
     * @return
     */
    @Override
    public CommonResult<List<StuClass>> getByName(String name) {
        List<StuClass> stuClassList = new ArrayList<>();
        PerStudent perStudent = iPerStudentService.getOne(new QueryWrapper<PerStudent>().lambda()
                .eq(PerStudent::getStudentKey, jwtTokenUtil.getUsernameByRequest(httpServletRequest)).last("limit 1"));

        // 学生返回自己的班级
        if (perStudent != null) {
            StuClass stuClass = this.getById(perStudent.getClassId());
            stuClassList.add(stuClass);
            return CommonResult.success(stuClassList);
        }

        // 不是学生不是老师
        PerTeacher perTeacher = iPerTeacherService.getOne(new QueryWrapper<PerTeacher>().lambda()
                .eq(PerTeacher::getTeacherKey, jwtTokenUtil.getUsernameByRequest(httpServletRequest)).last("limit 1"));
        if (perTeacher == null) {
            return CommonResult.success(stuClassList);
        }

        // 是否超级管理员 学工办 学工部主任
        if (perTeacher.getRoleInfo().contains(admin)
                || perTeacher.getRoleInfo().contains(office)
                || perTeacher.getRoleInfo().contains(officeMonitor)) {
            List<StuClass> stuClasses = this.list(new QueryWrapper<StuClass>().lambda()
                    .eq(StuClass::getStatus, 10)
                    .like(StrUtil.isNotEmpty(name), StuClass::getClassName, name));
            stuClassList.addAll(stuClasses);
            return CommonResult.success(stuClassList);
        }

        // 是否班主任
        if (perTeacher.getRoleInfo().contains(classMonitor)) {
            List<ClassTeacher> classTeacherList = iClassTeacherService.list(
                    new QueryWrapper<ClassTeacher>().lambda()
                            .eq(ClassTeacher::getTeacherKey, jwtTokenUtil.getUsernameByRequest(httpServletRequest))
            );
            if (classTeacherList.size() > 0) {
                List<Long> classIds = classTeacherList.stream().map(ClassTeacher::getClassId).collect(Collectors.toList());
                List<StuClass> stuClasses = this.list(new QueryWrapper<StuClass>().lambda()
                        .eq(StuClass::getStatus, 10)
                        .like(StrUtil.isNotEmpty(name), StuClass::getClassName, name)
                        .in(BaseEntity::getId, classIds));
                stuClassList.addAll(stuClasses);
            }
        }
        // 是否专业主任
        if (perTeacher.getRoleInfo().contains(subjectMonitor)) {
            String deptId = perTeacher.getUpperDept();
            List<StuClass> stuClasses = this.list(new QueryWrapper<StuClass>().lambda()
                    .eq(StuClass::getDepartmentId, deptId)
                    .like(StrUtil.isNotEmpty(name), StuClass::getClassName, name)
                    .eq(StuClass::getStatus, 10));
            if (stuClasses.size() > 0) {
                stuClassList.addAll(stuClasses);
            }
        }
        stuClassList = stuClassList.stream().distinct().collect(Collectors.toList());
        return CommonResult.success(stuClassList);
    }

    /**
     * 获取可查看年级
     *
     * @return
     */
    @Override
    public CommonResult<List<String>> getYearList() {
        return CommonResult.success(baseMapper.getYearList());
    }
}
